#!/bin/bash
#Convert Python Fetion To Shell @2012
IPADDR=`/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1'|grep -v '192.168' | cut -d: -f2 | awk '{ print $1}'`
msg1=$IPADDR" reports:"
DELL_SRV_DIR=/opt/dell/srvadmin
PATH=$PATH:$DELL_SRV_DIR/oma/bin:$DELL_SRV_DIR/bin:$DELL_SRV_DIR/sbin
#OMREPORT=`find $DELL_SRV_DIR -name omreport 2> /dev/null`
STOR_CTRL=/tmp/dell.storage.ctr
LOG_FILE=/tmp/dell_omreport.log
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKOWN=3
FETION_PATH=/opt/omreportsh
sendmail() {
if [ $# -ge 1 ]; then
      msg=$1
 else
      exit 0
 fi
echo $msg 
echo `date` 
##sendmail~~
if [ -f "$FETION_PATH/tosendlist.txt" ] && [ -f "$FETION_PATH/mail.jar" ];then
    for toSend in `cat $FETION_PATH/tosendlist.txt`
        do
            java -jar $FETION_PATH/mail.jar -msg "$msg" -to "$toSend" -sub "$IPADDR"
        done
else
    echo "Download needed files error" && exit 1
fi
echo 'send end!'
}

if [ ! -d $DELL_SRV_DIR ]; then
        echo "Please install OpenManger and define the PATH after DELL_SRV_DIR" && exit $STATE_UNKOWN
fi
/etc/init.d/dataeng status >> /dev/null
if [ ! $? -eq 0 ]; then
        echo "Please start the service dataeng" && exit $STATE_UNKOWN
fi
#check chassis
omreport chassis | grep ^[^Ok] | grep ":" | sed '/COMPONENT/d' > $LOG_FILE
#check storage
omreport storage controller | grep "^ID" | cut -d":" -f2 > $STOR_CTRL
if [ ! -s $STOR_CTRL ]; then
        echo "Have you installed the package for storage?" >> $LOG_FILE
fi
for CONTR_ID in `cat $STOR_CTRL`
do
        omreport storage controller controller=$CONTR_ID | grep -2 ^Status | sed '/--/d' | awk '{if (NR%5==0){print $0} else {printf"%s ",$0}}' | grep -v Ok | tr -s " *" " " >> $LOG_FILE
done
if [ -s $LOG_FILE ]; then
        paste -s $LOG_FILE > $LOG_FILE.2
        JAVA_HOME=/usr/local/jdk
        PATH=$JAVA_HOME/bin:$PATH
        export JAVA_HOME PATH
        if [ `grep -c "Critical" $LOG_FILE` -eq `grep -c "\-Critical" $LOG_FILE` ]; then
		msg2=`cat $LOG_FILE.2`
		msg2=$msg1$msg2
                echo $msg2 && sendmail "$msg2" && exit $STATE_WARNING
        else
		msg2=`cat $LOG_FILE.2`
		msg2=$msg1$msg2
		echo $msg2 && sendmail "$msg2" && exit $STATE_CRITICAL
        fi
else
        echo "Machine is Health" && exit $STATUS_OK
fi
